package com.warm.androidstudy.common;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.warm.androidstudy.R;
import com.warm.androidstudy.utils.LogUtil;
import com.warm.androidstudy.widget.CustomProgressDialog;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseFragment<VM extends BaseViewModel,VDB extends ViewDataBinding> extends Fragment {
    /**
     * 获取布局
     * @return
     */
    protected abstract int getLayoutId();
    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * viewModel
     */
    protected VM mViewModel;
    /**
     * dataBind绑定数据
     */
    protected VDB binding;
    protected CustomProgressDialog progressDialog = null;

    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        binding =  DataBindingUtil.inflate(inflater, getLayoutId(),container,false);
        View view = binding.getRoot();
        return view;
    }



    @Override
    public void onViewCreated( View view, Bundle savedInstanceState) {
        initViewModel();
        binding.setLifecycleOwner(this);
        initView();
        initData();
        super.onViewCreated(view, savedInstanceState);
    }


    /**
     * 初始化ViewModel
     */
    public void initViewModel(){
        if (mViewModel==null){
            Class modelClass;
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType){
                modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
            }else {
                //若是没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel.class;
            }
            mViewModel = (VM) new ViewModelProvider(this).get(modelClass);
        }
    }

    public void initProgressDialog() {
        if (null == progressDialog) {
            progressDialog = new CustomProgressDialog(getActivity(), "加载中...", R.drawable.customprogress_dialog_anim);
        }
    }

    public void showProgressDialog() {
        // 判断fragment是否依附Activity
        if (!isAdded()) {
            return;
        }
        if (null == progressDialog) {
            initProgressDialog();
        }
        if (!progressDialog.isShowing()) {
            try {
                progressDialog.show();
            } catch (Exception e) {
                LogUtil.e(e.getMessage());
            }
        }
    }


    public void dismissProgressDialog() {
        // 判断fragment是否依附Activity
        if (!isAdded()) {
            return;
        }
        if (null != progressDialog && progressDialog.isShowing()) {
            try {
                progressDialog.dismiss();
            } catch (Exception e) {
                LogUtil.e(e.getMessage());
            }
        }
    }
}
